package k.b.s;

import java.io.InputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import k.b.s.model.ChannelRowModel;
import k.b.s.model.ItemRowModel;

public class OperationDB {


	public static ArrayList<ChannelRowModel> getChannelTB(Context context){
		ArrayList<ChannelRowModel> rowModelArray = new ArrayList<ChannelRowModel>();
		final String tb = "CHANNEL_TB";
		SQLiteDatabase database = getRDB(context);

		Cursor cursor = database.query(tb, null, null ,null, null, null, null);

		if(cursor != null){
			while(cursor.moveToNext()){
				ChannelRowModel rowModel = new ChannelRowModel();
				rowModel.setChannel(cursor.getString(cursor.getColumnIndex("channel")));
				rowModel.setTitle(cursor.getString(cursor.getColumnIndex("title")));
				rowModel.setDescription(cursor.getString(cursor.getColumnIndex("description")));
				rowModelArray.add(rowModel);
			}
			cursor.close();
		}
		return rowModelArray;
	}

	public static ArrayList<ItemRowModel> getItemTB(Context context){
		ArrayList<ItemRowModel> rowModelArray = new ArrayList<ItemRowModel>();
		final String tb = "ITEM_TB";
		SQLiteDatabase database = getRDB(context);

		Cursor cursor = database.query(tb, null, null ,null, null, null, null);

		if(cursor != null){
			while(cursor.moveToNext()){
				ItemRowModel rowModel = new ItemRowModel();
				rowModel.setId(cursor.getInt(cursor.getColumnIndex("_id")));
				rowModel.setChannel(cursor.getString(cursor.getColumnIndex("channel")));
				rowModel.setTitle(cursor.getString(cursor.getColumnIndex("title")));
				rowModel.setDescription(cursor.getString(cursor.getColumnIndex("description")));
				rowModel.setPubdate(cursor.getInt(cursor.getColumnIndex("pubdate")));
				rowModel.setIsread(cursor.getInt(cursor.getColumnIndex("isread")));
				rowModelArray.add(rowModel);
			}
		}
		return rowModelArray;
	}

	public static ArrayList<ItemRowModel> getItemFromChannelbyPubDate(Context context,String channel,int i){
		ArrayList<ItemRowModel> rowModelArray = new ArrayList<ItemRowModel>();
		final String tb = "ITEM_TB";
		SQLiteDatabase database = getRDB(context);

		Cursor cursor = database.query(tb, null, "channel='" + channel + "'" ,null, null, null, "pubDate DESC");

		if(cursor != null){
			for(int j=0;j<i;j++){
				if(cursor.moveToNext()){
					ItemRowModel rowModel = new ItemRowModel();
					rowModel.setChannel(cursor.getString(cursor.getColumnIndex("channel")));
					rowModel.setTitle(cursor.getString(cursor.getColumnIndex("title")));
					rowModel.setDescription(cursor.getString(cursor.getColumnIndex("description")));
					rowModel.setPubdate(cursor.getInt(cursor.getColumnIndex("pubdate")));
					rowModel.setIsread(cursor.getInt(cursor.getColumnIndex("isread")));
					rowModelArray.add(rowModel);
				}
			}
		}
		return rowModelArray;
	}

	public static void addChannel(ChannelRowModel rowModel, SQLiteDatabase database, Context context){
		final String table ="CHANNEL_TB";
		if (rowModel.getTitle()!=null){
			if (rowModel != null) {
					if(!isInDatabase(rowModel.getTitle(),context,table)){
					ContentValues values = new ContentValues();
					values.put("channel", rowModel.getChannel());
					values.put("title", rowModel.getTitle());
					values.put("description", rowModel.getDescription());
					database.insert(table, null, values);
				}
			}

		}
	}

	public static void addItem(ArrayList<ItemRowModel> rowModels,SQLiteDatabase database,Context context){
		final String table = "ITEM_TB";
		if (rowModels != null) {
			for(ItemRowModel rowModel:rowModels){
				if(!isInDatabase(rowModel.getTitle(),context,table)){
					ContentValues values = new ContentValues();
					values.put("channel", rowModel.getChannel());
					values.put("title", rowModel.getTitle());
					values.put("description", rowModel.getDescription());
					values.put("pubdate", rowModel.getPubdate());
					values.put("isread", 0);
					database.insert(table, null, values);
				}
			}
		}
	}

	private static boolean isInDatabase(String title,Context context,String table){
		Database databaseOpenHelper = new Database(context);
		SQLiteDatabase database = null;
		String[] select = {"title"};

		database = databaseOpenHelper.getReadableDatabase();
		Cursor cursor = database.query(table, select, null ,null, null, null, null);

		if(cursor != null){
			while(cursor.moveToNext()){
				String titleInDB = cursor.getString(cursor.getColumnIndex("title"));
				if(title.equals(titleInDB)){
					if (databaseOpenHelper != null) {
						databaseOpenHelper.close();
					}
					return true;
				}
			}
		}
		if (databaseOpenHelper != null) {
			databaseOpenHelper.close();
		}
		return false;
	}

	public static SQLiteDatabase getWDB(Context context){
		Database databaseOpenHelper = new Database(context);
		SQLiteDatabase database = databaseOpenHelper.getWritableDatabase();
		return database;
	}

	public static SQLiteDatabase getRDB(Context context){
		Database databaseOpenHelper = new Database(context);
		SQLiteDatabase database = databaseOpenHelper.getReadableDatabase();
		return database;
	}

	public static void updateItemTB(Context context){
		ArrayList<ChannelRowModel> cTB = OperationDB.getChannelTB(context);
    	SQLiteDatabase database = OperationDB.getWDB(context);
    	for(int i=0;i<cTB.size();i++){
    		InputStream in = HttpGetter.getContent(cTB.get(i).getChannel());
    		ArrayList<ItemRowModel> rowModels = ItemXmlHelper.getInstance().parseSax(in,cTB.get(i).getChannel());
    		OperationDB.addItem(rowModels,database, context );
    	}
	}

	public static void dropItemTB(Context context){
		SQLiteDatabase database = getWDB(context);
		final String tb = "ITEM_TB";
		database.delete(tb, null, null);
	}

	public static void deleteChannel(String channel, Context context){
		SQLiteDatabase database = getWDB(context);
		database.delete("CHANNEL_TB","channel='" + channel + "'", null);
		database.delete("ITEM_TB","channel='" + channel + "'", null);
	}

	public static void setIsRead(ItemRowModel rowModel, Context context){
		SQLiteDatabase database = getWDB(context);
		String tb = "ITEM_TB";
		if (rowModel != null) {
				ContentValues values = new ContentValues();
				values.put("channel", rowModel.getChannel());
				values.put("title", rowModel.getTitle());
				values.put("description", rowModel.getDescription());
				values.put("pubdate", rowModel.getPubdate());
				values.put("isread", 1);
				database.update(tb, values, "_id="+rowModel.getId() , null);
		}
	}

	public static boolean isNotFull(Context context){
		SQLiteDatabase database = getRDB(context);
		String tb = "CHANNEL_TB";

		Cursor cursor = database.query(tb, null, null ,null, null, null, null);

		if(cursor != null){
			int i = cursor.getCount();
			if(i<5)
				return true;
		}
		return false;
	}

}

